<?php

namespace App\HttpController\Api;

use App\Model\Es\EsVideo;

class Search extends Base
{

    public function index()
    {
        $keyword = trim($this->params['keyword']);
        if(empty($keyword)){
            return $this->writeJson(200,'OK',$this->getPagingDatas(0,[],0));
        }
        $esObj = new EsVideo();
        $result = $esObj->searchName($keyword,'match',$this->params['from'],$this->params['size']);
        if(empty($result)){
            return $this->writeJson(200,'OK',$this->getPagingDatas(0,[],0));
        }
        $hits = $result['hits']['hits'];
        $total = $result['hits']['total'];
        foreach($hits as $hit){
            $source = $hit['_source'];
            $resData[] = [
                'id' => $hit['_id'],
                'name' => $source['name'],
                'image' => $source['image'],
                'uploader' => $source['uploader'],
                'create_time' => '',
                'keywords' => [$keyword],
            ];
        }
        return $this->writeJson(200,'OK',$this->getPagingDatas($total['value'],$resData,1));
        
    }

}